<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="artcleForm" ref="artcleFormRef" size="small" :inline="true">
      <el-form-item label="标题" prop="title">
        <el-input v-model="artcleForm.title" placeholder="请输入标题"></el-input>
      </el-form-item>

      <el-form-item label="资讯类别" prop="infoType">
        <el-select v-model="artcleForm.articleClass" placeholder="请选择资讯类别" clearable>
          <el-option v-for="(item, index) in infoType" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="资讯栏目" prop="column">
        <el-input v-model="artcleForm.typeidName" placeholder="请输入资讯栏目"></el-input>
      </el-form-item>

      <el-form-item label="所属高校" prop="schoolList">
        <el-select v-model="artcleForm.schoolId" placeholder="请选择所属高校" clearable>
          <el-option v-for="(item, index) in schoolList" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="artcleForm.status" placeholder="请选择状态" clearable>
          <el-option v-for="(item, index) in status" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮组 -->
    <el-row :gutter="1
    " class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <!-- 内容表格 -->
    <el-table :data="informationList">
      <el-table-column label="id" align="center" prop="informationId" />

      <el-table-column label="标题" align="center" prop="title" />

      <el-table-column label="来源" align="center" prop="source" />

      <el-table-column label="审核状态" align="center" prop="examineStatus">
        <template slot-scope="scope">
          {{
              scope.row.examineStatus != 0
                ? scope.row.examineStatus == 1
                  ? '审核通过'
                  : '审核失败'
                : '待审核'
          }}
        </template>
      </el-table-column>

      <el-table-column label="启用状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? '禁用' : '启用' }}
        </template>
      </el-table-column>

      <el-table-column label="资讯类别" align="center" prop="articleClass">
        <template slot-scope="scope">
          {{ handleInfoClassify(scope.row.articleClass) }}
        </template>
      </el-table-column>

      <el-table-column label="缩略图" align="center" prop="image">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>

      <el-table-column label="资讯栏目" align="center" prop="columnId">
        <template slot-scope="scope">
          {{ handelInfoColumnClassidy(scope.row.columnId) }}
        </template>
      </el-table-column>

      <el-table-column label="是否推荐" align="center" prop="isRecommend">
        <template slot-scope="scope">
          {{ scope.row.isRecommend == 0 ? '不推荐' : '推荐' }}
        </template>
      </el-table-column>

      <el-table-column label="是否轮播推荐" align="center" width="100" prop="rotationRecommend">
        <template slot-scope="scope">
          {{ scope.row.rotationRecommend == 0 ? '不轮播' : '轮播' }}
        </template>
      </el-table-column>

      <el-table-column label="是否置顶" align="center" prop="isTopping">
        <template slot-scope="scope">
          {{ scope.row.isTopping == 0 ? '不置顶' : '置顶' }}
        </template>
      </el-table-column>

      <el-table-column label="所属高校" align="center" prop="schoolId">
      </el-table-column>

      <el-table-column label="收藏数" align="center" prop="collectionNum">
      </el-table-column>

      <el-table-column label="评论数" align="center" prop="commentNum">
      </el-table-column>

      <el-table-column label="点赞数" align="center" prop="likeNum">
      </el-table-column>

      <el-table-column label="是否搜索推荐" align="center" width="100" prop="isSearchRecommend">
        <template slot-scope="scope">
          {{ scope.row.isSearchRecommend == 0 ? '不推荐' : '推荐' }}
        </template>
      </el-table-column>

      <el-table-column label="踩一踩数" align="center" prop="stepNum">
      </el-table-column>

      <el-table-column label="标签" align="center" prop="lable">
      </el-table-column>

      <el-table-column label="排序" align="center" prop="sort">
      </el-table-column>

      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="selectContent(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" @click="toggleStatus(scope.row)">{{
              scope.row.status == 1 ? '禁用' : '启用'
          }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <el-pagination @current-change="handleCurrentChange" :current-page="queryParams.pageNum" :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="infoDetailedListRef" :model="infoDetailedList" :rules="infoDetailedListRules" label-width="100px" :disabled="formDisabled">
        <el-form-item label="标题" prop="title">
          <el-input v-model="infoDetailedList.title" placeholder="请输入标题" />
        </el-form-item>

        <el-form-item label="来源" prop="source">
          <el-input v-model="infoDetailedList.source" placeholder="请输入来源" />
        </el-form-item>

        <el-form-item label="关键词" prop="keywords">
          <el-input v-model="infoDetailedList.keywords" placeholder="请输入关键词" />
        </el-form-item>

        <el-form-item label="简介" prop="synopsis">
          <el-input v-model="infoDetailedList.synopsis" placeholder="请输入简介" />
        </el-form-item>

        <el-form-item label="启用状态" prop="status">
          <el-select v-model="infoDetailedList.status" placeholder="启用">
            <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="资讯类别" prop="articleClass">
          <el-select v-model="infoDetailedList.articleClass" placeholder="请选择资讯类别">
            <el-option v-for="item in infoType" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="缩略图" prop="image">
          <image-upload v-model="infoDetailedList.image" />
        </el-form-item>

        <el-form-item label="缩略图" prop="image">
          <image-upload v-model="infoDetailedList.image" />
        </el-form-item>

        <el-form-item label="资讯栏目" prop="columnId">
          <el-select v-model="infoDetailedList.columnId" placeholder="请选择资讯栏目">
            <el-option v-for="item in columnId" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否推荐" prop="isRecommend">
          <el-select v-model="infoDetailedList.isRecommend" placeholder="请选择">
            <el-option v-for="item in isRecommend" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否轮播推荐" prop="rotationRecommend">
          <el-select v-model="infoDetailedList.rotationRecommend" placeholder="请选择">
            <el-option v-for="item in isRecommend" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否置顶" prop="isTopping">
          <el-select v-model="infoDetailedList.isTopping" placeholder="请选择">
            <el-option v-for="item in isTopping" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所属学校" prop="schoolId">
          <el-select v-model="infoDetailedList.schoolId" placeholder="请选择">
            <el-option v-for="item in schoolId" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否搜索推荐" prop="isSearchRecommend">
          <el-select v-model="infoDetailedList.isSearchRecommend" placeholder="请选择">
            <el-option v-for="item in isRecommend" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="浏览量" prop="browseNum">
          <el-input v-model="infoDetailedList.browseNum" placeholder="请输入浏览量" />
        </el-form-item>

        <el-form-item label="标签" prop="lable">
          <el-input v-model="infoDetailedList.lable" placeholder="请输入标签" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input v-model="infoDetailedList.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('infoDetailedListRef')" v-if="defineBtn">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getInfoList, addInfo, toggleStatus, updateInfo, deleteInfo, getEligibleInfo } from '@/api/fetv/info.js'
export default {
  data() {
    return {
      columnList: {},
      // 搜索表单
      artcleForm: {
        title: '',
        articleClass: undefined,
        typeidName: '',
        schoolId: '',
        status: undefined
      },

      schoolList: [
        {
          label: '福州工商学院',
          value: 1
        },
        {
          label: '福州软件职业技术学院',
          value: 2
        }
      ],

      status: [
        {
          label: '启用',
          value: 1
        },
        {
          label: '禁用',
          value: 2
        }
      ],

      infoType: [
        {
          label: '新闻资讯',
          value: 1
        },
        {
          label: '视频',
          value: 2
        },
        {
          label: '外联',
          value: 3
        }
      ],

      isRecommend: [
        {
          label: '不推荐',
          value: 0
        },
        {
          label: '推荐',
          value: 1
        }
      ],

      isTopping: [
        {
          label: '不置顶',
          value: 0
        },
        {
          label: '置顶',
          value: 1
        }
      ],

      articleClass: [
        {
          label: '首页',
          value: 1
        },
        {
          label: '头条新闻',
          value: 2
        },
        {
          label: '要闻聚集',
          value: 3
        }
      ],

      columnId: [],

      // 学校
      schoolId: [
        {
          label: '福州大学',
          value: 1
        },
        {
          label: '泉州大学',
          value: 2
        },
        {
          label: '福建师范大学',
          value: 3
        }
      ],

      // 资讯表格内容
      informationList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },

      // 总条数
      total: 0,

      // 弹出框的标题
      title: '',

      // 控制弹出框的线上
      open: false,

      // 弹出框的数据
      infoDetailedList: {},

      // 弹出框中确定按钮的显示
      defineBtn: false,

      infoDetailedListRules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        source: [{ required: true, message: '来源不能为空', trigger: 'blur' }],
        articleClass: [{ required: true, message: '资讯类别不能为空', trigger: 'blur' }],
        columnId: [
          {
            required: true,
            message: '资讯栏目不能为空',
            trigger: 'blur'
          }
        ]
      },

      // 是否禁用表单
      formDisabled: false,

      // 当前行的栏目Id和状态
      thisRowId: 0,
      thisRowStatus: 0
    }
  },

  methods: {
    //归零
    format() {
      this.infoDetailedList = {}
      // 解除表单禁用
      this.formDisabled = false
    },
    // 搜索
    handleQuery() {
      getEligibleInfo(this.artcleForm).then(response => {
        this.informationList = response.rows
        this.total = response.total
        this.$message.success('搜索成功')
      })
    },

    // 重置
    resetQuery() {
      this.artcleForm = {}
      this.getInfoList()
    },

    // 新增数据
    handleAdd() {
      this.title = '添加资讯管理'
      this.open = true
      this.defineBtn = true
      this.infoDetailedList = {}
    },

    // 修改资讯管理
    handleUpdate(item) {
      this.title = '修改资讯管理'
      this.open = true
      this.infoDetailedList = item

      // 不再禁用表单
      this.formDisabled = false
      // 显示确定按钮
      this.defineBtn = true
    },

    // 删除对话框
    handleDelete(item) {
      this.$confirm(`是否确认删除资讯管理编号为${item.informationId}的数据`, '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteInfo(item.informationId)
          this.getReviewList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 获取资讯内容
    getInfoList() {
      getInfoList(this.queryParams).then(response => {
        this.informationList = response.rows
        this.total = response.total
      })
    },

    // 将资讯进行分类
    handleInfoClassify(typeId) {
      switch (typeId) {
        case 1:
          return '新闻资讯'
        case 2:
          return '视频'
        case 3:
          return '外联'
        default:
          break
      }
    },

    // 将资讯栏目进行分类
    handelInfoColumnClassidy(id) {
      switch (id) {
        case 1:
          return '首页'
        case 2:
          return '头条新闻'
        case 3:
          return '要闻聚集'
        case 4:
          return '校园'
        case 5:
          return 'FETV'
        case 6:
          return '招考'
        case 7:
          return '空中课堂'
        case 8:
          return '家长'
        case 9:
          return '师说'
        case 10:
          return '职场'
        case 11:
          return '微视频'
        default:
          return '更多'
      }
    },

    // 查看当前行的数据
    selectContent(item) {
      this.title = '查看资讯管理'
      this.open = true
      this.infoDetailedList = item

      // 禁用掉表单
      this.formDisabled = true
    },

    // 状态的切换
    toggleStatus(item) {
      this.$confirm(`是否确认${item.status == 1 ? '禁用' : '启用'}资讯管理编号为${item.informationId}的数据`, '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          toggleStatus({
            informationId: item.informationId,
            status: item.status == 0 ? 1 : 0
          }).then(response => {
            if (response.code == 200) {
              this.$message.success('修改成功')
              this.getInfoList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },

    // 提交按钮
    submitForm(refName) {
      this.$refs[refName].validate(valid => {
        if (valid) {
          if (this.title == '添加资讯管理') {
            addInfo(this.infoDetailedList).then(response => {
              this.$message.success('新增成功')
              this.getInfoList()
              this.open = false
            })
          } else {
            updateInfo(this.infoDetailedList).then(response => {
              this.$message.success('修改成功'), this.getInfoList()
              this.open = false
            })
          }
        }
      })
    },

    // 修改页码的同时进行页面刷新
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getInfoList()
    }
  },
  created() {
    this.getInfoList()
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item--mini.el-form-item {
  margin-left: -52px;
  display: inline-block;
}

::v-deep .el-pagination {
  float: right;
  margin-top: 20px;
}
</style>
